<?php
/**
 * Created by PhpStorm.
 * User: yandian
 * Date: 16/8/6
 * Time: 11:22
 */

namespace app\weixin\controller;

use app\common\controller\LiteBase;
use app\common\util\weixin\JsApiTool;
use app\weixin\services\WxpayService;

class Wxpay extends LiteBase
{
    /**
     * @var WxpayService
     */
    protected $_service;

    protected function _initialize()
    {
        $this->_service = new WxpayService();
    }

    public function index()
    {

        $billId = I('get.id', 0, 'intval');
        if ($billId > 0) {
            $billRow = \app\common\model\Donation::instance()->where(['id' => $billId])->find();
            if (!empty($billRow)) {
                if ($billRow['status'] == 1) {
                    die('您不能支付已经支付过的订单');
                }
                $money = intval($billRow['money']);
                if ($money <= 0) {
                    die('金额不能小于零');
                }

                $data = ['billId' => $billId];
                $payinfo = $this->_service->getPayInfo($money, $data);
                if ($payinfo['error'] == 1) {
                    exit($payinfo['msg']);
                }
                $this->assign('id',$billId);
                $this->assign('money', 10);
                $this->assign('JsSignPack', JsApiTool::getSignPackage());
                $this->assign('jsApiParameters', $payinfo['info']);
                return $this->fetch();
            }
        }
    }


    public function payNotify()
    {
        $this->_service->payNotify();
    }

    public function payresult()
    {
        return $this->fetch();
    }
}